package com.zte.sys.position.control;

import com.zte.sys.login.control.WebSocketServer;
import com.zte.sys.login.model.LUsers;
import com.zte.sys.login.model.Permission;
import com.zte.sys.login.service.UserLoginService;
import com.zte.sys.modules.model.MModule;
import com.zte.sys.modules.model.MModuleCondition;
import com.zte.sys.position.model.Job;
import com.zte.sys.position.model.JobCondition;
import com.zte.sys.position.service.IJobService;
import com.zte.util.OperatorLog;
import com.zte.util.PageModel;
import com.zte.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Controller
public class JobAction {

    @Autowired
    private UserLoginService userLoginService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IJobService jobService;

    //分页查询
    @RequestMapping("/job/pageJob")
    @ResponseBody
    @OperatorLog(operate="分页条件查询", module="职位管理")
    public ResultMessage pageJob(JobCondition conn, PageModel<Job> pageModel) {
        PageModel<Job> jobs = jobService.queryJobPageByCondition(conn,pageModel);
        ResultMessage rm = new ResultMessage();
        if (jobs != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(jobs);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        return rm;
    }

    @RequestMapping("/job/jobsList")
    @ResponseBody
    public ResultMessage queryJobListByCondition(JobCondition conn){
        List<Job> jobs = jobService.queryJobListByCondition(conn); ResultMessage rm = new ResultMessage();
        if (jobs != null) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(jobs);
        } else {
            rm.setFlag(false);
            rm.setStatus(500);
            rm.setMsg("查询失败！");
        }
        return rm;

    }

    //详情
    @RequestMapping("/job/jobDetail")
    @ResponseBody
    public ResultMessage queryJobByJobId(String jobId) throws Exception{
        System.out.println(jobId);
        Job job = null;
        if(jobId != null && jobId != ""){
           job = jobService.queryJobByJobId(jobId);
        }
        ResultMessage rm = new ResultMessage();
        if (job != null) {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("用户已存在！");
            rm.setData(job);
        } else if(jobId == null){
            rm.setFlag(false);
            rm.setStatus(401);
            rm.setMsg("请输入用户编号！");
        }else if(jobId != null && !jobId.equals("")){
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("成功");
        }
        System.out.println(rm);
        return rm;
    }

    //添加
    @RequestMapping("sys/job/positionAdd")
    public String jobAdd(){
        return "sys/position/positionAdd";
    }
    @RequestMapping("/job/jobAdd")
    @ResponseBody
    @OperatorLog(operate="添加职位", module="职位管理")
    public ResultMessage addJob(Job job){
        int i = jobService.addJob(job);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("添加成功！");
            rm.setData(job);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("添加失败！");
        }
        return rm;
    }

    //修改
    @RequestMapping("sys/job/positionUpdate")
    public String jobUpdate(){
        return "sys/position/positionUpdate";
    }
    @RequestMapping("/job/updateJob")
    @ResponseBody
    @OperatorLog(operate="修改职位", module="职位管理")
    public ResultMessage updateJob(Job job){
        int i = jobService.updateJob(job);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("修改成功！");
            rm.setData(job);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("修改失败！");
        }
        return rm;
    }

    //状态修改
    @RequestMapping("/job/updateJobState")
    @ResponseBody
    @OperatorLog(operate="修改职位状态", module="职位管理")
    public ResultMessage updateJobState(Job job){
        int i = jobService.updateJobState(job);
        ResultMessage rm = new ResultMessage();
        if (i == 1) {
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("修改成功！");
            rm.setData(job);
        } else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("修改失败！");
        }
        return rm;
    }

    //赋权
    @RequestMapping("sys/job/positionGrant")
    public String positionGrant(){
        return "sys/position/positionGrant";
    }
    @RequestMapping("/job/moduleList2")
    @ResponseBody
    public ResultMessage queryModuleMap(JobCondition conn){
        Map<String, List<MModule>> stringListMap = jobService.queryModuleMap(conn);
        ResultMessage rm = new ResultMessage();
        if(stringListMap!=null){
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("查询成功！");
            rm.setData(stringListMap);
        }else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("查询失败！");
        }
        return rm;
    }

    /**
     * 删除所有权限
     * @param comrid
     * @return
     */
    @RequestMapping("/job/delPermission")
    @ResponseBody
    public ResultMessage delPermission(String comrid){
        int i = jobService.delPermission(comrid);
        ResultMessage rm = new ResultMessage();
        if(i == 1){
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("删除成功！");
        }else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("删除失败！");
        }
        return rm;
    }

    /**
     * 重新赋权
     * @param permission
     * @return
     */
    @RequestMapping("/job/addPermission")
    @ResponseBody
    @OperatorLog(operate="职位赋权", module="职位管理")
    public ResultMessage addPermission(Permission permission){

        int i = jobService.addPermission(permission);

        ResultMessage rm = new ResultMessage();
        if(i == 1){
            rm.setFlag(true);
            rm.setStatus(200);
            rm.setMsg("添加成功！");
            ConcurrentHashMap<String, WebSocketServer> webSocketMap = WebSocketServer.getWebSocketMap();
            printTime(webSocketMap);

        }else {
            rm.setFlag(false);
            rm.setStatus(400);
            rm.setMsg("添加失败！");
        }
        return rm;
    }

    public  void  printTime(ConcurrentHashMap<String,WebSocketServer> webSocketMap){

        Enumeration<String> keys = webSocketMap.keys();
        while (keys.hasMoreElements()){
            String userId = keys.nextElement();
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            String date = dateFormat.format(new Date());
            LUsers user =(LUsers) redisTemplate.opsForValue().get(userId);
            if(user==null){
                return;
            }
            LUsers users = userLoginService.detailOfUser(user);
            if(user.getPermissions().size()!=users.getPermissions().size()){
                redisTemplate.opsForValue().set(userId, users, 30, TimeUnit.MINUTES);
                WebSocketServer.sendInfo(date,userId);
            }
            else {
                for(int i=0;i<users.getPermissions().size();i++){
                    int j=0;
                    for(j=0;j<user.getPermissions().size();j++){
                        if(users.getPermissions().get(i).getCompid()==user.getPermissions().get(j).getCompid()){
                            break;
                        }
                    }
                    if(j==user.getPermissions().size()){
                        redisTemplate.opsForValue().set(userId, users, 30, TimeUnit.MINUTES);
                        WebSocketServer.sendInfo(date,userId);
                    }
                }
            }
        }
    }

}
